"""

SecondBarging相关的路由方法

"""
from typing import List, Dict

from fastapi import APIRouter
from numpy.ma.core import empty


from Agent.Dispatch.Dao.DispatchRequest import DispatchRequest
from Agent.Dispatch.Service.DispatchService import DispatchService
from Agent.FillTable.Dao.FillTableRequest import FillTableRequest
from Agent.FillTable.Service.FillTableService import FillTableService
from Agent.FormAnalyzer.Service.FormAnalyzerService import FormAnalyzerServiceService
from Agent.SecondBarging.Dao.SecondBargainingRequest import SecondBargainingRequest
from Agent.SecondBarging.Dao.SecondBargainingResponse import SecondBargainingResponse
from Agent.SecondBarging.Service.SecondBargingService import SecondBargingService
from LlmModel.DoubaoModel.DoubaoModel import DoubaoModel
from utils import logger
from utils.chat import getUserInput, convert_userInput_to_formDict
from utils.logger import get_logger

SecondBargingController = APIRouter(prefix="/secondbargaining")
secondBargingService = SecondBargingService()
fillTableService = FillTableService()
formAnalyzerServiceService = FormAnalyzerServiceService()
logger = get_logger("SecondBargingController")
doubao_client = DoubaoModel()
baseTable = """1.达人昵称:Doki宋宋（孕期）\n2.达人主页链接:https://www.xiaohongshu.com/user/profile/596e638850c4b475bc3139ce\n3.品牌/产品名称:伊利QQ星\n4.产品品类:伊利奶粉\n5.推广周期:（12月20日前发布）\n6.投放形式:（报备视频80% 报备图文20%）\n7.报备图文价格:\n8.报备视频价格:\n9.是否可以保价到次月:\n10.折扣要求:（30%）\n11.创作方向:（单品种草）\n12.排竞要求:（前 15 后 15）\n13.寄样方式:（寄拍）\n14.出镜小孩性别/年龄:\n15.达人是否处于孕期:\n16.达人场景体验匹配度:\n17.可保阅读数据:（视频保底1万播放，图文保底7000阅读）\n18.可保互动数据:（点赞+评论≥400）\n19.是否可以免费授权品牌信息流 6 个月？:\n20.是否可以免费授权品牌二剪及全平台传播（包括电商）6 个月？:\n21.是否可以免费分发其他平台？:\n22.是否有特殊合作注意事项？（如广告占比要求、脚本修改次数等）:\n23.是否免费授权电商平台植入 6 个月？:\n24.是否授权搜索引导6 个月？:\n25.是否授权带相关话题6 个月？:\n26.是否授权组件需求6 个月？:\n27.是否授权引流需求6 个月？:"""


@SecondBargingController.post("/process")
async def SencondBargainingProcess(request: SecondBargainingRequest) -> SecondBargainingResponse:
    """
    处理SecondBarging请求
    :param request: SecondBargainingRequest json表单（具体的示例在SecondBargainingRequest中有）
    :return:  SecondBargainingResponse  json响应
    """

    """
    处理(f==1&&s="确认合作")  &&  (f==1&&s="已拒品") 两种情况
    因为过来聊天的博主可能是之前已经洽谈过的，可能是已经确认合作的和聊失败的，要先把这两种情况的请求踢出去
    """
    if request.is_first == 1:
        if request.cooperation_status == "确认合作":
            logger.info("检测到is_first=1且cooperation_status=确认合作，直接返回特定响应并跳过后续流程")
            return SecondBargainingResponse(
                form=request.form,
                agent_response="宝子，我们已经将您作为意向合作对象，过段时间会找您的！",
                status="0",
                category=request.category,  # 拒绝原因
                subcategory=request.subcategory,  # 小类
                cooperation_status=request.cooperation_status,  # 合作状态大类
                is_first=request.is_first
            )
        elif request.cooperation_status == "已拒品":
            logger.info("检测到is_first=1且cooperation_status=已拒品，直接返回空响应并跳过后续流程")
            return SecondBargainingResponse(
                form=request.form,
                agent_response="宝子，这边正在争取给到您满意的权益，确认后工作人员会联系您呢，辛苦啦~",
                status="0",
                category=request.category,  # 拒绝原因
                subcategory=request.subcategory,  # 小类
                cooperation_status=request.cooperation_status,  # 合作状态大类
                is_first=request.is_first
            )

    """
            ---------------------------------dispatch分支判断---------------------------------------
            core：根据和用户的conversation判断聊天状态，然后走不同的处理分支
            简单来说就是判断：用户是在和我们做二次议价（商单细节洽谈，状态A1，下面主要处理的内容） 还是在闲聊状态（状态A2）
            ---------------------------------------------------------------------------------------  
    """
    dispatch_request = DispatchRequest(
        conversations=request.conversations
    )

    dispatchService = DispatchService()
    dispatch_response = await dispatchService.fill_bargin_dispatch_route(dispatch_request)

    if not dispatch_response.success:
        return SecondBargainingResponse(
            form=request.form,
            agent_response="好的宝",
            status="1",
            is_first=request.is_first
        )
    route_code = dispatch_response.route_code
    logger.info(f"Dispatch agent returned route code: {route_code}")

    if route_code == "1":
        return await secondBargingService.handle_fill_agent(request)
    # elif route_code == "2":
    #     return await _handle_global_qa(request)
    # elif route_code == "3":
    #     return SecondBargainingResponse(
    #         agent_response="好的,清单已确认,后续将按此推进合作",
    #         form=request.form,
    #         status="0",
    #         category="无",  # 拒绝原因
    #         subcategory="已确认",  # 小类
    #         cooperation_status="确认合作",  # 合作状态大类
    #         is_first=request.is_first
    #     )
    else:
        return SecondBargainingResponse(
            form=request.form,
            agent_response="好的 宝子，想要跟我谈合作可以直接填表哦：\n" + baseTable,
            status="0",
            is_first=request.is_first
        )
